package com.ny.view;

import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class IndexView {
    ProductService  productService = new ProductServiceImpl();

    public IndexView() throws SQLException {
    }
    OrderService orderService = new OrderServiceImpl();
    public void indexView(Student student) throws SQLException, ClassNotFoundException {
        System.out.println("请选择相应操作");
        System.out.println("1.个人信息界面");
        System.out.println("2.查看商品信息");
        System.out.println("3.查看订单信息");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        switch (choice){
            case 1:
                System.out.println("1.个人信息界面");
                System.out.println("学生ID：" + student.getStudentId());
                System.out.println("学生姓名：" + student.getStudentName());
                System.out.println("学生电话：" + student.getStudentPhone());
                System.out.println("1.修改密码");
                System.out.println("2.修改手机号");
                System.out.println("请输入你的选择");
                int choice1 = scanner.nextInt();
                switch (choice1){
                    case 1:
                        StudentService studentService = new StudentServiceImpl();
                        System.out.println("请输入你的新密码");
                        String newPassword = scanner.next();
                        int i = studentService.updatePassword(student.getStudentId(),newPassword);
                        if (i>0){
                            System.out.println("修改成功");
                        }else {
                            System.out.println("修改失败");
                        }

                        break;
                        case 2:
                            StudentService studentService1 = new StudentServiceImpl();
                            System.out.println("请输入你的新手机号");
                            String newPhone = scanner.next();
                            student.setStudentPhone(newPhone);
                            int a = studentService1.updatePhone(student.getStudentId(),newPhone);
                            if (a>0){
                                System.out.println("修改成功");
                            }


                }
                break;



            case 2:
                System.out.println("2.商品信息界面");
                List<Product> productList = productService.findAllProduct();
                System.out.println("商品序号"+"\t"+"商品名称"+"\t"+"商品价格"+"\t"+"商品库存"+"\t"+"商品类别"+"\t"+"商品描述");
                for (Product product : productList){
                    System.out.println("\t");
                    System.out.print(product.getProduct_id());
                    System.out.print("\t"+"\t");
                    System.out.print(product.getName());
                    System.out.print("\t"+"\t");
                    System.out.print(product.getPrice());
                    System.out.print("\t"+"\t");
                    System.out.print(product.getStock());
                    System.out.print("\t"+"\t");
                    System.out.print(product.getCategory());
                    System.out.print("\t"+"\t");
                    System.out.print(product.getDescription());
                }
                System.out.println();
                System.out.println("请输入你需要购买的商品序号");
                int productId = scanner.nextInt();
                System.out.println("请输入你需要购买的商品数量");
                String productNum = scanner.next();
               int sid=  student.getStudentId();
               double total_price = productService.findAllProduct().get(productId-1).getPrice()*Integer.parseInt(productNum);
               Order oder =new Order();
               oder.setStudent_id(sid);
               oder.setProduct_id(productId);
               oder.setNumber(productNum);
               oder.setTotal_price(total_price);
               int i = orderService.addOrder(oder);
               if (i>0){
                   System.out.println("添加成功");
               }
                break;
            case 3:
                System.out.println("3.订单信息界面");
                List<Order> orderList = orderService.findAllOrder();
                System.out.println("订单序号"+"\t"+"学生号"+"\t"+"商品号"+"\t"+"购买数量"+"\t"+"订单总价");
                for (Order order : orderList){
                    System.out.println("\t");
                    System.out.print(order.getOrder_id());
                    System.out.print("\t"+"\t");
                    System.out.print(order.getStudent_id());
                    System.out.print("\t"+"\t");
                    System.out.print(order.getProduct_id());
                    System.out.print("\t"+"\t");
                    System.out.print(order.getNumber());
                    System.out.print("\t"+"\t");
                    System.out.print(order.getTotal_price());
                }
                System.out.println();
                break;
        }
    }
}
